package com.sxt;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *****************************************************************************
 * DESC ：聊天客户端. 
 * Author : 利宝项目组 
 * CREATEDATE ：2012-2-12 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-2-12		Created.
 ****************************************************************************
 **/
/**
 * 聊天客户端.
 * <br/> 下午8:42:18 2012-2-12
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
/**
 * @author Administrator
 * 
 */
public class ChatClient extends Frame {

	private TextField tf = new TextField();

	private TextArea ta = new TextArea();

	private Socket s = null;
	
	private DataOutputStream dos = null;
	
	private DataInputStream dis = null;
	
	private boolean bConnected = false;

	public static void main(String[] args) {
		ChatClient chatClient = new ChatClient();
		chatClient.launchFrame();
	}

	public void launchFrame() {
		this.setLocation(400, 300);
		this.setSize(300, 300);
		add(tf, BorderLayout.SOUTH);
		add(ta, BorderLayout.NORTH);
		pack();
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				disconnect();
				System.exit(0);
			}
		});
		tf.addActionListener(new TFListener());
		this.setVisible(true);
		connect();
		new Thread(new RecvThread()).start();
	}

	public void connect() {
		try {
			s = new Socket("127.0.0.1", 8888);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
			System.out.println("---Connected!---");
			bConnected = true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void disconnect(){
		try {
			dos.close();
			dis.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private class TFListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String str = tf.getText().trim();
			// ta.setText(str);
			tf.setText("");
			try {
				// DataOutputStream dos = new DataOutputStream(s.getOutputStream());
				dos.writeUTF(str);
				dos.flush();
				// dos.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	private class RecvThread implements Runnable{
		@Override
		public void run() {
			try{
				while(bConnected){
					String str = dis.readUTF();
					ta.setText(ta.getText() + str + "\n");
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
	}
}
